﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ObservadorPeplau.Usercontrols
{
    public partial class Loading : UserControl
    {
        private Control _parent = null;

        public Loading()
        {
            InitializeComponent();
        }

        public void ShowLoading(Control parentControl)
        {
            // Remove from previous parent
            if (_parent!=null)
                CloseLoading();

            // Add to a new parent
            _parent = parentControl.Parent;
            _parent.Controls.Add(this);

            Width = parentControl.Width;
            Height = parentControl.Height;
            Top = parentControl.Top;
            Left = parentControl.Left;

            SetImagePosition();
            BringToFront();
        }

        public void CloseLoading()
        {
            if (_parent == null)
                return;

            _parent.Controls.Remove(this);
            _parent = null;
        }

        private void SetImagePosition()
        {
            picLoader.Top = (Height - picLoader.Height) / 2;
            picLoader.Left = (Width - picLoader.Width) / 2;
            
            picLoader.Top = int.Parse(((Height - picLoader.Height) / 2).ToString());
            picLoader.Left = int.Parse(((Width - picLoader.Width)/2).ToString());
        }

        private void Loading_Resize(object sender, EventArgs e)
        {
            SetImagePosition();
        }
    }
}
